package com.doit.day01;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
 * 监听节点中值的变化
 */

public class WatcherDemo {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {

        //创建zk的对象
        ZooKeeper zk = new ZooKeeper("linux01:2181", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("就单纯的整一个监听器");
            }
        });

        byte[] data = zk.getData("/teacher", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                byte[] data = new byte[0];
                try {
                    data = zk.getData("/teacher", this, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(new String(data));
            }
        }, null);

//        System.out.println(new String(data));

        Thread.sleep(Integer.MAX_VALUE);

    }
}
